﻿package kgame5.pbe.animation {
	import flash.utils.Dictionary;

	/**
	 *
	 * @author Panayoti
	 * @description Utility class for creating a few AnimationPlayers that can be created easily
	 *
	 */
	public class AnimationPlayerFactory
	{
		private var _animations:Dictionary;

		public function AnimationPlayerFactory()
		{
			_animations=new Dictionary();
		}

		/**
		 * Adds a new AnimationPlayer to the factory
		 * @param name a String used to reference the AnimationPlayer
		 * @param animationPlayer The instance of the AnimationPlayer to add to the factory
		 *
		 */
		public function register(name:String, animationPlayer:AnimationPlayer):void
		{
			if (_animations[name])return;
			_animations[name]=animationPlayer;
		}

		/**
		 * Create a new AnimationPlayer
		 * @param name The reference to the AnimationPlayer you would like to create
		 * @return A new instance of the AnimationPlayer
		 *
		 */
		public function instantiate(name:String):AnimationPlayer
		{
			if (!_animations[name])return null;
			var animationPlayer:AnimationPlayer=_animations[name];
			return animationPlayer.copy();
		}

		/**
		 * Cleans up and removes all references to the AnimationPlayer and trashes this factory.
		 */
		public function dispose():void
		{
			for (var key:String in _animations)
			{
				var animationPlayer:AnimationPlayer=_animations[key];
				delete _animations[key];
			}

			_animations=null;
		}

	}
}